#!/usr/bin/env python3
"""
简单测试dashboard API的脚本
"""
import json
import sys
import os
from datetime import datetime

# 添加当前目录到路径
sys.path.insert(0, os.path.dirname(__file__))

from idea_proxy import IDEAProxy
from dashboard import ProxyDashboard

def test_api():
    """测试API数据"""
    # 创建代理实例
    proxy = IDEAProxy('127.0.0.1', 8080)
    
    # 添加测试数据
    test_request = {
        'timestamp': datetime.now().isoformat(),
        'client_addr': '127.0.0.1:12345',
        'method_line': 'GET https://plugins.jetbrains.com/api/plugins HTTP/1.1',
        'headers': {
            'User-Agent': 'IntelliJ IDEA/2023.3',
            'Host': 'plugins.jetbrains.com',
            'Accept': 'application/json'
        },
        'raw_request': 'GET https://plugins.jetbrains.com/api/plugins HTTP/1.1\nUser-Agent: IntelliJ IDEA/2023.3'
    }
    
    proxy.requests_log.append(test_request)
    
    print(f"代理日志中的请求数: {len(proxy.requests_log)}")
    print(f"请求内容: {json.dumps(proxy.requests_log[0], indent=2, ensure_ascii=False)}")
    
    # 测试API响应
    requests_data = {
        'requests': proxy.requests_log[-100:],
        'timestamp': datetime.now().timestamp(),
        'total_count': len(proxy.requests_log)
    }
    
    print(f"\nAPI将返回的数据:")
    print(f"- requests数组长度: {len(requests_data['requests'])}")
    print(f"- total_count: {requests_data['total_count']}")
    print(f"- 第一个请求的method_line: {requests_data['requests'][0]['method_line']}")

if __name__ == '__main__':
    test_api()